package simulador;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;

import org.omg.CORBA.PUBLIC_MEMBER;

public class Teste {
	public static void toHSV(BufferedImage i) {
		float[] hsbvals = new float[3];

		for (int x = 0; x < i.getWidth(); x++) {
			for (int y = 0; y < i.getHeight(); y++) {
				int rgb = i.getRGB(x, y);
				Color c = new Color(rgb);

				// hsbvals[0] = hue;
				// hsbvals[1] = saturation;
				// hsbvals[2] = brightness;

				Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), hsbvals);
				Color nc = new Color(hsbvals[0], hsbvals[1], hsbvals[2]);

				i.setRGB(x, y, nc.getRGB());
			}
		}
	}
	
	public static void toHSVgray(BufferedImage i, int color) {
		float[] hsbvals = new float[3];

		for (int x = 0; x < i.getWidth(); x++) {
			for (int y = 0; y < i.getHeight(); y++) {
				int rgb = i.getRGB(x, y);
				Color c = new Color(rgb);

				// hsbvals[0] = hue;
				// hsbvals[1] = saturation;
				// hsbvals[2] = brightness;

				Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), hsbvals);
				Color nc = new Color(hsbvals[color], hsbvals[color], hsbvals[color]);

				i.setRGB(x, y, nc.getRGB());
			}
		}
	}

	public static void toCMY(BufferedImage i) {
		for (int x = 0; x < i.getWidth(); x++) {
			for (int y = 0; y < i.getHeight(); y++) {
				int rgb = i.getRGB(x, y);
				Color c = new Color(rgb);
				int red = 255 - c.getRed();
				int blue = 255 - c.getBlue();
				int green = 255 - c.getGreen();
				Color nc = new Color(red, green, blue);
				i.setRGB(x, y, nc.getRGB());
			}
		}
	}

	public static void toCMYgray(BufferedImage i, int color) {
		for (int x = 0; x < i.getWidth(); x++) {
			for (int y = 0; y < i.getHeight(); y++) {
				int rgb = i.getRGB(x, y);
				Color c = new Color(rgb);
				int[] cmyvals = new int[3]; 
				cmyvals[0] = 255 - c.getRed();
				cmyvals[1]= 255 - c.getBlue();
				cmyvals[2]= 255 - c.getGreen();
				Color nc = new Color(cmyvals[color], cmyvals[color], cmyvals[color]);
				i.setRGB(x, y, nc.getRGB());
			}
		}
	}
	
	public static void toYCRCB(BufferedImage i) {
		for (int x = 0; x < i.getWidth(); x++) {
			for (int z = 0; z < i.getHeight(); z++) {
				int rgb = i.getRGB(x, z);
				Color c = new Color(rgb);				
				int y = (int) (16 +  (65.738/256* c.getRed()) + 129.057/256*c.getGreen() + 25.064/256*c.getBlue());
				int cb= (int) (128 - (37.945/256* c.getRed()) - 74.494/256*c.getGreen() + 112.439/256*c.getBlue());
				int cr= (int) (128 + (112.493/256*c.getRed()) - 94.154/256*c.getGreen() - 18.285/256*c.getBlue());
				Color nc = new Color(y, cb, cr);
				i.setRGB(x, z, nc.getRGB());
			}
		}
	}
	
	public static void toYCRCBgray(BufferedImage i, int color) {
		for (int x = 0; x < i.getWidth(); x++) {
			for (int z = 0; z < i.getHeight(); z++) {
				int rgb = i.getRGB(x, z);
				Color c = new Color(rgb);
				int[] ycrcbvals = new int[3]; 
				ycrcbvals[0] = (int) (16 +  (65.738/256* c.getRed()) + 129.057/256*c.getGreen() + 25.064/256*c.getBlue());
				ycrcbvals[1]= (int) (128 - (37.945/256* c.getRed()) - 74.494/256*c.getGreen() + 112.439/256*c.getBlue());
				ycrcbvals[2]= (int) (128 + (112.493/256*c.getRed()) - 94.154/256*c.getGreen() - 18.285/256*c.getBlue());
				Color nc = new Color(ycrcbvals[color], ycrcbvals[color], ycrcbvals[color]);
				i.setRGB(x, z, nc.getRGB());
			}
		}
	}
	
	

	public static void main(String[] args) throws IOException {
		BufferedImage i = ImageIO.read(Teste.class
				.getResourceAsStream("resource/lena.jpg"));
		long l = System.currentTimeMillis();
		toHSV(i);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("hsv.jpg"));
		
		// Cinza do HSV *********************************************************
		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toHSVgray(i,0);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("hsvh.jpg"));
		
		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toHSVgray(i,1);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("hsvs.jpg"));
		
		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toHSVgray(i,2);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("hsvv.jpg"));
		//***********************************************************************
		
		// Cinza do CMY *********************************************************
		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toCMYgray(i,0);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("cmyc.jpg"));
		
		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toCMYgray(i,1);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("cmym.jpg"));
		
		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toCMYgray(i,2);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("cmyy.jpg"));
		//***********************************************************************
		
		// Cinza do YCrCb *********************************************************
		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toYCRCBgray(i,0);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("ycrcrby.jpg"));
		
		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toYCRCBgray(i,1);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("ycrcbcr.jpg"));
		
		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toYCRCBgray(i,2);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("ycrcbcb.jpg"));
		//***********************************************************************
		
		
		
		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toCMY(i);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("cmy.jpg"));

		i = ImageIO.read(Teste.class.getResourceAsStream("resource/lena.jpg"));
		l = System.currentTimeMillis();
		toYCRCB(i);
		System.out.println(System.currentTimeMillis() - l);
		ImageIO.write(i, "jpg", new File("ycrcb.jpg"));

	}
}
